﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarInfor.DAL;

namespace CarInfor.UI
{
    public partial class frmInsurance : Form
    {
        InsuranceForm insuranceForm = new InsuranceForm();//编辑用
        private ActionType curActionType;//当前操作类型
        private string key;//主键ID

        public frmInsurance(ActionType actionType, string id)
        {
            InitializeComponent();
            curActionType = actionType;
            key = id;
            Init();

            if (curActionType == ActionType.New)
            {
                this.Text = "保单信息--新建";
                this.chkJQ.Checked = true;
                this.chkBuss.Checked = false;
                this.panBuss.Enabled = true;
                this.panBuss.Enabled = false;
            }
            else if (curActionType == ActionType.Edit)
            {
                this.Text = "保单信息--编辑";
                this.chkJQ.Checked = false;
                this.chkBuss.Checked = false;
                this.panBuss.Enabled = false;
                this.panBuss.Enabled = false;
                insuranceForm.QueryWithKey(key);
                BindText(insuranceForm);
            }
        }

        /// <summary>
        /// 交强险保单号输入重复性验证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void txtInsuranceNumber_Validating(object sender, CancelEventArgs e)
        {
            string sql = string.Empty;
            if (curActionType == ActionType.New)
            {
                sql = " InsuranceNumber='" + this.txtInsuranceNumber.Text.Trim() + "'";
            }
            else if (curActionType == ActionType.Edit)
            {
                sql = " InsuranceNumber='" + this.txtInsuranceNumber.Text.Trim() + "' and ID<>'" + insuranceForm.ID + "'";
            }
            InsuranceForm inf = new InsuranceForm();
            if (inf.Query(sql).Rows.Count > 0)
            {
                MessageHelper.ShowInfo("交强险保单号（" + this.txtInsuranceNumber.Text.Trim() + " ）已经存在！");
                e.Cancel = true;
            }
        }

        /// <summary>
        /// 商业保险保单号输入验证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void txtBussInsuranceNumber_Validating(object sender, CancelEventArgs e)
        {
            string sql = string.Empty;
            if (curActionType == ActionType.New)
            {
                sql = " BussInsuranceNumber='" + this.txtBussInsuranceNumber.Text.Trim() + "'";
            }
            else if (curActionType == ActionType.Edit)
            {
                sql = " BussInsuranceNumber='" + this.txtBussInsuranceNumber.Text.Trim() + "' and ID<>'" + insuranceForm.ID + "'";
            }

            InsuranceForm inf = new InsuranceForm();
            if (inf.Query(sql).Rows.Count > 0)
            {
                MessageHelper.ShowInfo("商业险保单号（" + this.txtBussInsuranceNumber.Text.Trim() + " ）已经存在！");
                e.Cancel = true;
            }
        }

        /// <summary>
        /// 车牌号码输入验证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void txtCarNumber_Validating(object sender, CancelEventArgs e)
        {
            if (curActionType == ActionType.New)
            {
                if (!string.IsNullOrEmpty(this.txtCarNumber.Text.Trim()) && this.cboCarType.SelectedIndex != 0)
                {
                    string sql = " CarNumber='" + this.txtCarNumber.Text.Trim() + "' and CarType='" + this.cboCarType.SelectedItem.ToString() + "'";
                    InsuranceForm inf = new InsuranceForm();
                    if (inf.Query(sql).Rows.Count > 0)
                    {
                        if (MessageHelper.ShowYesNoChoice("检测到车牌号（" + this.txtCarNumber.Text.Trim() + this.cboCarType.SelectedItem.ToString() + "）已经存在，是否进入编辑模式？") == DialogResult.Yes)
                        {
                            key = inf.Query(sql).Rows[0]["编号"].ToString();
                            curActionType = ActionType.Edit;
                            this.Text = "保单信息--编辑";
                            this.chkJQ.Checked = false;
                            this.chkBuss.Checked = false;
                            this.panBuss.Enabled = false;
                            this.panBuss.Enabled = false;
                            insuranceForm.QueryWithKey(key);
                            BindText(insuranceForm);
                        }
                        else
                        {
                            e.Cancel = true;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 编辑数据绑定
        /// </summary>
        /// <param name="insuranceForm"></param>
        private void BindText(InsuranceForm insuranceForm)
        {
            this.txtOwnerName.Text = insuranceForm.OwnerName;
            dtpStartDate.Text = insuranceForm.StartDate;
            dtpEndDate.Text = insuranceForm.EndDate;
            if (!string.IsNullOrEmpty(insuranceForm.InsuranceNumber))
            {
                this.chkJQ.Checked = true;
                this.panJQ.Enabled = true;
                this.txtInsuranceNumber.Text = insuranceForm.InsuranceNumber;
                dtpStartDate.Text = insuranceForm.StartDate;
                dtpEndDate.Text = insuranceForm.EndDate;
            }
            else
            {
                this.chkJQ.Checked = false;
                this.panJQ.Enabled = false;
            }

            if (!string.IsNullOrEmpty(insuranceForm.BussInsuranceNumber))
            {
                this.chkBuss.Checked = true;
                this.panBuss.Enabled = true;
                this.txtBussInsuranceNumber.Text = insuranceForm.BussInsuranceNumber;
                dtpBussStartDate.Text = insuranceForm.BussStartDate;
                dtpBussEndDate.Text = insuranceForm.BussEndDate;
            }
            else
            {
                this.chkBuss.Checked = false;
                this.panBuss.Enabled = false;
            }
            this.txtCarNumber.Text = insuranceForm.CarNumber;
            this.txtTel.Text = insuranceForm.Telephone;
            this.cboCarType.SelectedItem = insuranceForm.CarType;
            this.txtFrameNumber.Text = insuranceForm.FrameNumber;
            this.dtpBaoHuDate.Text = insuranceForm.BaoHuDate;
            this.cboUseProperty.SelectedItem = insuranceForm.UseProperty;
            this.nudWeight.Value = insuranceForm.Weight;
            this.comUnit.SelectedItem = insuranceForm.Unit;
            //this.lblTotal.Text = insuranceForm.Total.ToString();
            this.txtMemo.Text = insuranceForm.Memo;

            InsuranceDetail insuranceDetail = new InsuranceDetail();
            DataTable dt = insuranceDetail.Query(" FormID='" + insuranceForm.ID + "'");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListViewItem li = new ListViewItem();
                li.SubItems[0].Text = dt.Rows[i]["险种名称"].ToString();
                if (dt.Rows[i]["保额"].ToString() == string.Empty)
                {
                    li.SubItems.Add("0.00");
                }
                else
                {
                    li.SubItems.Add(decimal.Parse(dt.Rows[i]["保额"].ToString()).ToString("0.00"));
                }
                li.SubItems.Add(decimal.Parse(dt.Rows[i]["金额"].ToString()).ToString("0.00"));
                this.listView1.Items.Add(li);
            }
            UpdateHJ();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        void Init()
        {
            BindCarType();
            BindUseProperty();
            this.comUnit.SelectedIndex = 0;
            this.txtInsuranceNumber.SelectionStart = this.txtInsuranceNumber.Text.Length;

            this.dtpStartDate.Value = DateTime.Now;
            this.dtpBussStartDate.Value = DateTime.Now;
            this.dtpEndDate.Value = Common.GetDateNextYearDay(this.dtpStartDate.Value);
            this.dtpBussEndDate.Value = Common.GetDateNextYearDay(this.dtpBussStartDate.Value);

            this.txtCarNumber.Validating += new CancelEventHandler(txtCarNumber_Validating);
            this.txtBussInsuranceNumber.Validating += new CancelEventHandler(txtBussInsuranceNumber_Validating);
            this.txtInsuranceNumber.Validating += new CancelEventHandler(txtInsuranceNumber_Validating);
        }

        /// <summary>
        /// 绑定车辆类型
        /// </summary>
        private void BindCarType()
        {
            this.cboCarType.Items.Clear();
            CarType carType = new CarType();
            DataTable dt = carType.Query();
            this.cboCarType.Items.Add("--请选择--");
            foreach (DataRow row in dt.Rows)
            {
                this.cboCarType.Items.Add(row["类型名称"].ToString());
            }
            this.cboCarType.SelectedIndex = 0;
        }

        /// <summary>
        /// 绑定使用性质
        /// </summary>
        private void BindUseProperty()
        {
            this.cboUseProperty.Items.Clear();
            CarUseProperty carUseProperty = new CarUseProperty();
            DataTable dt = carUseProperty.Query();
            this.cboUseProperty.Items.Add("--请选择--");
            foreach (DataRow row in dt.Rows)
            {
                this.cboUseProperty.Items.Add(row["使用性质名称"].ToString());
            }
            this.cboUseProperty.SelectedIndex = 0;
        }

        /// <summary>
        /// 保存之前数据验证
        /// </summary>
        /// <returns></returns>
        bool CheckInput()
        {
            if (string.IsNullOrEmpty(this.txtCarNumber.Text.Trim()) || this.txtCarNumber.Text.Trim() == "陕C")
            {
                MessageHelper.ShowInfo("请输入车辆号牌！");
                this.txtCarNumber.Focus();
                this.txtCarNumber.SelectionStart = this.txtCarNumber.Text.Length;
                return false;
            }

            if (this.cboCarType.SelectedIndex == 0)
            {
                MessageHelper.ShowInfo("请输入车辆类型！");
                return false;
            }

            if (curActionType == ActionType.Edit)
            {
                InsuranceForm newObj = new InsuranceForm();
                DataTable dt = newObj.Query(" CarNumber='" + this.txtCarNumber.Text.Trim() + "'and CarType='" + this.cboCarType.SelectedItem.ToString() + "' and ID<>'" + key + "'");
                if (dt.Rows.Count > 0)
                {
                    MessageHelper.ShowInfo("车牌号(" + this.txtCarNumber.Text.Trim() + this.cboCarType.SelectedItem.ToString() + ")已经存在！");
                    return false;
                }
            }

            if ((!this.chkJQ.Checked) && (!this.chkBuss.Checked))
            {
                MessageHelper.ShowInfo("请选择一个保险类别！");
                return false;
            }
            if (this.chkJQ.Checked)
            {
                if (this.txtInsuranceNumber.Text.Trim() == "")
                {
                    MessageHelper.ShowInfo("请输入交强险保单号码！");
                    this.txtInsuranceNumber.Focus();
                    return false;
                }
                string sql = string.Empty;
                if (curActionType == ActionType.New)
                {
                    sql = " InsuranceNumber='" + this.txtInsuranceNumber.Text.Trim() + "'";
                }
                else if (curActionType == ActionType.Edit)
                {
                    sql = " InsuranceNumber='" + this.txtInsuranceNumber.Text.Trim() + "' and ID<>'" + insuranceForm.ID + "'";
                }
                InsuranceForm inf = new InsuranceForm();
                if (inf.Query(sql).Rows.Count > 0)
                {
                    MessageHelper.ShowInfo("交强险保单号（" + this.txtInsuranceNumber.Text.Trim() + " ）已经存在！");
                    return false;
                }
            }
            if (this.chkBuss.Checked)
            {
                if (this.txtBussInsuranceNumber.Text.Trim() == "")
                {
                    MessageHelper.ShowInfo("请输入商业险保单号码！");
                    this.txtBussInsuranceNumber.Focus();
                    return false;
                }
                string sql = string.Empty;
                if (curActionType == ActionType.New)
                {
                    sql = " BussInsuranceNumber='" + this.txtBussInsuranceNumber.Text.Trim() + "'";
                }
                else if (curActionType == ActionType.Edit)
                {
                    sql = " BussInsuranceNumber='" + this.txtBussInsuranceNumber.Text.Trim() + "' and ID<>'" + insuranceForm.ID + "'";
                }
                InsuranceForm inf = new InsuranceForm();
                if (inf.Query(sql).Rows.Count > 0)
                {
                    MessageHelper.ShowInfo("商业险保单号（" + this.txtBussInsuranceNumber.Text.Trim() + " ）已经存在！");
                    return false;
                }
            }


            if (this.txtOwnerName.Text.Trim() == "")
            {
                MessageHelper.ShowInfo("请输入被保险人姓名！");
                this.txtOwnerName.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(this.txtTel.Text.Trim()))
            {
                MessageHelper.ShowInfo("请输入联系电话！");
                this.txtTel.Focus();
                return false;
            }

            return true;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckInput())
            {
                return;
            }
            try
            {
                if (curActionType == ActionType.New)
                {
                    InsuranceForm newObj = new InsuranceForm();
                    newObj.ID = Guid.NewGuid().ToString();
                    newObj.OwnerName = this.txtOwnerName.Text.Trim();
                    if (this.chkJQ.Checked)
                    {
                        newObj.InsuranceNumber = this.txtInsuranceNumber.Text.Trim();
                        newObj.StartDate = dtpStartDate.Text.Trim();
                        newObj.EndDate = dtpEndDate.Text.Trim();
                    }
                    if (this.chkBuss.Checked)
                    {
                        newObj.BussInsuranceNumber = this.txtBussInsuranceNumber.Text.Trim();
                        newObj.BussStartDate = dtpBussStartDate.Text.Trim();
                        newObj.BussEndDate = dtpBussEndDate.Text.Trim();
                    }
                    newObj.CarNumber = this.txtCarNumber.Text.Trim();
                    newObj.Telephone = this.txtTel.Text.Trim();
                    newObj.CarType = this.cboCarType.SelectedItem.ToString().Equals("--请选择--") ? string.Empty : this.cboCarType.SelectedItem.ToString();
                    newObj.FrameNumber = this.txtFrameNumber.Text.Trim();
                    newObj.BaoHuDate = this.dtpBaoHuDate.Text.Trim();
                    newObj.UseProperty = this.cboUseProperty.SelectedItem.ToString().Equals("--请选择--") ? string.Empty : this.cboUseProperty.SelectedItem.ToString();
                    newObj.Weight = this.nudWeight.Value;
                    newObj.Unit = this.comUnit.SelectedItem.ToString();
                    newObj.Total = decimal.Parse(this.lblTotal.Text);
                    newObj.Memo = this.txtMemo.Text.Trim();
                    newObj.Insert();
                    for (int i = 0; i < this.listView1.Items.Count; i++)
                    {
                        InsuranceDetail insuranceDetail = new InsuranceDetail();
                        insuranceDetail.ID = Guid.NewGuid().ToString();
                        insuranceDetail.FormID = newObj.ID;
                        insuranceDetail.TypeName = this.listView1.Items[i].SubItems[0].Text;
                        insuranceDetail.InsuredAmount = decimal.Parse(this.listView1.Items[i].SubItems[1].Text);
                        insuranceDetail.Price = decimal.Parse(this.listView1.Items[i].SubItems[2].Text);
                        insuranceDetail.Insert();
                    }
                    this.DialogResult = DialogResult.OK;
                }
                else if (curActionType == ActionType.Edit)
                {
                    insuranceForm.InsuranceNumber = this.txtInsuranceNumber.Text.Trim();
                    insuranceForm.OwnerName = this.txtOwnerName.Text.Trim();
                    insuranceForm.StartDate = dtpStartDate.Text.Trim();
                    insuranceForm.EndDate = dtpEndDate.Text.Trim();
                    if (this.chkJQ.Checked)
                    {
                        insuranceForm.InsuranceNumber = this.txtInsuranceNumber.Text.Trim();
                        insuranceForm.StartDate = dtpStartDate.Text.Trim();
                        insuranceForm.EndDate = dtpEndDate.Text.Trim();
                    }
                    else
                    {
                        insuranceForm.InsuranceNumber = "";
                        insuranceForm.StartDate = "";
                        insuranceForm.EndDate = "";
                    }

                    if (this.chkBuss.Checked)
                    {
                        insuranceForm.BussInsuranceNumber = this.txtBussInsuranceNumber.Text.Trim();
                        insuranceForm.BussStartDate = dtpBussStartDate.Text.Trim();
                        insuranceForm.BussEndDate = dtpBussEndDate.Text.Trim();
                    }
                    else
                    {
                        insuranceForm.BussInsuranceNumber = "";
                        insuranceForm.BussStartDate = "";
                        insuranceForm.BussEndDate = "";
                    }
                    insuranceForm.CarNumber = this.txtCarNumber.Text.Trim();
                    insuranceForm.Telephone = this.txtTel.Text.Trim();
                    insuranceForm.CarType = this.cboCarType.SelectedItem.ToString().Equals("--请选择--") ? string.Empty : this.cboCarType.SelectedItem.ToString();
                    insuranceForm.FrameNumber = this.txtFrameNumber.Text.Trim();
                    insuranceForm.BaoHuDate = this.dtpBaoHuDate.Text.Trim();
                    insuranceForm.UseProperty = this.cboUseProperty.SelectedItem.ToString().Equals("--请选择--") ? string.Empty : this.cboUseProperty.SelectedItem.ToString();
                    insuranceForm.Weight = this.nudWeight.Value;
                    insuranceForm.Unit = this.comUnit.SelectedItem.ToString();
                    insuranceForm.Total = decimal.Parse(this.lblTotal.Text);
                    insuranceForm.Memo = this.txtMemo.Text.Trim();
                    insuranceForm.UpdateWithKey(key);

                    //先删除
                    InsuranceDetail insuranceDe = new InsuranceDetail();
                    insuranceDe.DeleteWithCondition("FormID='" + key + "' ");

                    //再添加
                    for (int i = 0; i < this.listView1.Items.Count; i++)
                    {
                        InsuranceDetail insuranceDetail = new InsuranceDetail();
                        insuranceDetail.ID = Guid.NewGuid().ToString();
                        insuranceDetail.FormID = key;
                        insuranceDetail.TypeName = this.listView1.Items[i].SubItems[0].Text;
                        insuranceDetail.InsuredAmount = decimal.Parse(this.listView1.Items[i].SubItems[1].Text);
                        insuranceDetail.Price = decimal.Parse(this.listView1.Items[i].SubItems[2].Text);
                        insuranceDetail.Insert();
                    }
                    this.DialogResult = DialogResult.OK;
                }
            }
            catch (Exception ex)
            {
                MessageHelper.ShowFail("保存失败！" + ex.Message);
            }
        }

        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmInsuranceItemSelect fs = new frmInsuranceItemSelect();
            if (fs.ShowDialog() == DialogResult.OK)
            {
                //List<string> nameList = GetNameList();

                List<InsuranceItem> lst = fs.GetSelectedItem();
                if (lst.Count != 0)
                {
                    foreach (InsuranceItem it in lst)
                    {
                        //if (!nameList.Contains(it.ItemName))
                        //{
                        ListViewItem li = new ListViewItem();
                        li.SubItems[0].Text = it.ItemName;
                        li.SubItems.Add(it.InsuredAmount.ToString("0.00"));
                        li.SubItems.Add(it.Price.ToString("0.00"));

                        this.listView1.Items.Add(li);
                        //}
                    }
                }
            }
            UpdateHJ();
        }

        /// <summary>
        /// 计算合计金额
        /// </summary>
        void UpdateHJ()
        {
            if (this.listView1.Items.Count != 0)
            {
                Decimal JE = 0;
                for (int i = 0; i < this.listView1.Items.Count; i++)
                {
                    JE += Decimal.Parse(this.listView1.Items[i].SubItems[2].Text);
                }

                this.lblTotal.Text = JE.ToString();
            }
            else
            {
                this.lblTotal.Text = "0.00";
            }
        }

        /// <summary>
        /// 移除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count != 0)
            {
                this.listView1.Items.Remove(this.listView1.SelectedItems[0]);
            }
            UpdateHJ();
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count != 0)
            {
                ListViewItem listItem = this.listView1.SelectedItems[0];
                frmItemEdit fi = new frmItemEdit(listItem.SubItems[0].Text, listItem.SubItems[1].Text, listItem.SubItems[2].Text);
                if (fi.ShowDialog() == DialogResult.OK)
                {
                    listItem.SubItems[0].Text = fi.txtName.Text.Trim();
                    listItem.SubItems[1].Text = fi.txtInsuredAmount.Text.Trim();
                    listItem.SubItems[2].Text = fi.txtPrice.Text.Trim();
                }
            }
            UpdateHJ();
        }

        /// <summary>
        /// 输入检查
        /// </summary>
        /// <returns></returns>
        bool checkInput()
        {
            if (this.txtName.Text.Trim() == "")
            {
                MessageHelper.ShowInfo("请输入名称！");
                this.txtName.Focus();
                return false;
            }

            try
            {
                if (this.txtInsuredAmount.Text.Trim() == "")
                {
                    //MessageBox.Show("请输入保额！");
                    //this.txtInsuredAmount.Focus();
                    //return false;
                    this.txtInsuredAmount.Text = "0.00";
                }
                Decimal.Parse(this.txtInsuredAmount.Text);
            }
            catch
            {
                MessageHelper.ShowInfo("保额输入有误！");
                this.txtInsuredAmount.Focus();
                return false;
            }

            try
            {
                if (this.txtPrice.Text.Trim() == "")
                {
                    MessageHelper.ShowInfo("请输入金额！");
                    this.txtPrice.Focus();
                    return false;
                }
                Decimal.Parse(this.txtPrice.Text);
            }
            catch
            {
                MessageHelper.ShowInfo("金额输入有误！");
                this.txtPrice.Focus();
                return false;
            }


            return true;
        }

        /// <summary>
        /// 手动添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!checkInput())
                return;
            //List<string> nameList = GetNameList();
            //if (!nameList.Contains(this.txtName.Text.Trim()))
            //{
            ListViewItem li = new ListViewItem();
            li.SubItems[0].Text = this.txtName.Text.Trim();
            li.SubItems.Add(this.txtInsuredAmount.Text);
            li.SubItems.Add(this.txtPrice.Text);
            this.listView1.Items.Add(li);

            this.txtName.Text = string.Empty;
            this.txtInsuredAmount.Text = string.Empty;
            this.txtPrice.Text = string.Empty;
            //}
            //else
            //{
            //    MessageBox.Show("列表中已经存在！");
            //                }
            UpdateHJ();
        }

        /// <summary>
        /// 输入正确性验证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 46)
            {

                e.Handled = true;
            }
            else if (e.KeyChar == 46 && txtPrice.Text.IndexOf(".") >= 0)
            {
                e.Handled = true;
            }
            else if (e.KeyChar == 13)
            {
                txtCsJe_Validating(sender, new CancelEventArgs());
            }
        }

        /// <summary>
        ///  输入正确性验证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtCsJe_Validating(object sender, CancelEventArgs e)
        {
            TextBox tb = sender as TextBox;
            try
            {
                Decimal.Parse(tb.Text);
            }
            catch
            {
                return;
            }

            if (tb.Text.IndexOf(".") == -1)
            {
                tb.Text = tb.Text + ".00";
                return;
            }
            else if (tb.Text.IndexOf(".") == 0)
            {
                tb.Text = "0" + tb.Text;
            }
            else
            {
                int i = tb.Text.IndexOf(".") + 3;
                tb.Text = tb.Text.Substring(0, i < tb.Text.Length ? i : tb.Text.Length);
            }
            int clen = tb.Text.Length;
            for (int i = 0; i < 3 - (clen - tb.Text.IndexOf(".")); i++)
            {
                tb.Text = tb.Text + "0";
            }
        }

        /// <summary>
        /// 获取列表中所有项名称
        /// </summary>
        /// <returns></returns>
        private List<string> GetNameList()
        {
            List<string> nameList = new List<string>();
            for (int i = 0; i < this.listView1.Items.Count; i++)
            {
                nameList.Add(this.listView1.Items[i].SubItems[0].Text);
            }
            return nameList;
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 交强险选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkJQ_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkJQ.Checked)
            {
                this.panJQ.Enabled = true;
            }
            else
            {
                this.panJQ.Enabled = false;
            }
        }

        /// <summary>
        /// 商业险选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkBuss_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkBuss.Checked)
            {
                this.panBuss.Enabled = true;
            }
            else
            {
                this.panBuss.Enabled = false;
            }

        }

        private void cboCarType_Validating(object sender, CancelEventArgs e)
        {
            if (curActionType == ActionType.New)
            {
                if (!string.IsNullOrEmpty(this.txtCarNumber.Text.Trim()) && this.cboCarType.SelectedIndex != 0)
                {
                    string sql = " CarNumber='" + this.txtCarNumber.Text.Trim() + "' and CarType='" + this.cboCarType.SelectedItem.ToString() + "'";
                    InsuranceForm inf = new InsuranceForm();
                    if (inf.Query(sql).Rows.Count > 0)
                    {
                        if (MessageHelper.ShowYesNoChoice("检测到车牌号（" + this.txtCarNumber.Text.Trim() + this.cboCarType.SelectedItem.ToString() + "）已经存在，是否进入编辑模式？") == DialogResult.Yes)
                        {
                            key = inf.Query(sql).Rows[0]["编号"].ToString();
                            curActionType = ActionType.Edit;
                            this.Text = "保单信息--编辑";
                            this.chkJQ.Checked = false;
                            this.chkBuss.Checked = false;
                            this.panBuss.Enabled = false;
                            this.panBuss.Enabled = false;
                            insuranceForm.QueryWithKey(key);
                            BindText(insuranceForm);
                        }
                        else
                        {
                            e.Cancel = true;
                        }
                    }
                }
            }
        }

        private void dtpStartDate_ValueChanged(object sender, EventArgs e)
        {
            this.dtpEndDate.Value = Common.GetDateNextYearDay(this.dtpStartDate.Value);
        }

        private void dtpBussStartDate_ValueChanged(object sender, EventArgs e)
        {
            this.dtpBussEndDate.Value = Common.GetDateNextYearDay(this.dtpBussStartDate.Value);
        }
    }
}
